package no.ntnu.fp.messages.impl;

import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.MessageCode;
import no.ntnu.fp.model.Appointment;
import no.ntnu.fp.utils.XmlParser;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DeleteAppointmentMessage implements Message {

	private final Appointment appointment;
	
	public DeleteAppointmentMessage(Appointment appointment) {
		this.appointment = appointment;
	}

	public DeleteAppointmentMessage() {
		this(null);
	}

	public Appointment getAppointment() {
		return appointment;
	}

	@Override
	public MessageCode getMessageCode() {
		return MessageCode.DELETE_APPOINTMENT_MESSAGE;
	}

	@Override
	public Message fromXmlString(String xml) {
		Document doc = XmlParser.parseXmlString(xml);
		Appointment app = new Appointment((Element)doc.getElementsByTagName("appointment").item(0));
		return new DeleteAppointmentMessage(app);
	}

	@Override
	public String toString() {
		Document doc = XmlParser.getThreadLocalDocumentBuilder().newDocument();
		Element messageElement = doc.createElement("message");
		messageElement.appendChild(appointment.toXml(doc));
		return getMessageCode() + XmlParser.convertXmlToString(messageElement);
	}

}
